package com.jiang.daily.Q781;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/04/20 18:55
 */
class Solution {
    // public int numRabbits(int[] answers) {
    //     int res = 0;
    //     Map<Integer, Integer> map = new HashMap<>();
    //     for (int answer : answers) {
    //         map.put(answer, map.getOrDefault(answer, 0) + 1);
    //     }
    //     for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
    //         int key = entry.getKey() + 1;
    //         int value = entry.getValue();
    //         // res += value / key * key + (value % key != 0 ? key : 0);
    //         res += (value + key - 1) / key * key;
    //     }
    //     return res;
    // }

    public int numRabbits(int[] answers) {
        int res = 0;
        int[] map = new int[1001];
        for (int answer : answers) {
            if (map[answer] == 0) res += answer + 1;
            map[answer]++;
            if (map[answer] > answer) map[answer] = 0;
        }
        return res;
    }
}
